package com.software.human.util;

import androidx.annotation.NonNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtil {
    private static OkHttpClient okHttpClient;

    public static OkHttpClient getInstance(String token) {
        if (okHttpClient == null) {
//
//            okHttpClient = new OkHttpClient();
            // 通过拦截器增加token或其他内容
            okHttpClient = new OkHttpClient().newBuilder()
                    .addInterceptor(new Interceptor() {
                        @NonNull
                        @Override
                        public Response intercept(@NonNull Chain chain) throws IOException {
                            Request request = chain.request().newBuilder()
                                    .addHeader("X-token", token)
                                    .build();
                            Response response = chain.proceed(request);
                            // 需要鉴权
                            return response;
                        }
                    })
                    .build();
        }
        return okHttpClient;
    }
}
